const { hottoplist, messageiconList } = require('../../utils/api')
const { fetch } = require('../../utils/fetch')
import { Base } from '../../utils/base.js';
var base = new Base();
Page({
    data: {
        active: 0,
        prePageData: null,
        toplist:[],
        list: [],
        num: 1,
        type: null,
        sonType: 0,
        hasMore: 0,//正在加载
        hasMoreData:0,//没有更多数据
        loadFlag: true,
        master_type_id:0,//主分类
        che_yz_show:0,
        che_yzbtn_show:0,
        specificarea:null,
    },

    onLoad: function(options) {
        const data = JSON.parse(options.data)
        const type = data.id;
      var sonType = data.childList.length ? data.childList[0].id : 0
        this.setData({
            prePageData: data,
            type,
            sonType: sonType,
            master_type_id:data.id,
            che_yz_show: data.id==13?1:0,
            che_yzbtn_show: data.id == 13 ? 1 : 0
        })
        if(wx.getStorageSync('specificarea')) {
          this.setData({
            specificarea:wx.getStorageSync('specificarea')
          })
        }
        wx.setNavigationBarTitle({
            title: data.title
        })
        // wx.hideShareMenu()

        this.getData()

       // 首页置顶付费
        fetch(`wx/message/${this.data.specificarea}/toplist`, 'POST', {
            type:this.data.type,
           }).then(res=>{
            if(res) {
                this.setData({
                  toplist:res.toplist
                })
            } 
        })
    },

    getData() {
      this.setData({
          hasMore: 1,//正在加载
        })
        // wx.showNavigationBarLoading()
        // wx.showLoading({
        //     title: '加载中...'
        // })
        fetch(`wx/message/${this.data.specificarea}/list`, 'POST', {
          pageSize: 10,
          pageNumber: this.data.num,
          oneType:this.data.type,
          sonType: this.data.sonType
      }).then(res => {
            // wx.hideNavigationBarLoading()
            // wx.hideLoading()
            // wx.stopPullDownRefresh()
            if (this.data.num == 1) {
                this.setData({
                    list: []
                })
            }
            if(res==null){
              res={"list":[]}
            }
        if(res.list) {
          var len=res.list.length;
          for (let i = 0;i < len; i++) {
            if(res.list[i]["remark"]!=null){
              res.list[i]["remark_arr"] = res.list[i]["remark"].split(',');
            }else{
              res.list[i]["remark_arr"]='';
            }
            
            var content = base.jsonToObj(res.list[i]["content"]);
            if (this.data.type==13){
              if (content.startAddress){
                var startAddress = content.startAddress.split(',');
                content.startAddress = startAddress[2];
              }
              if (content.endAddress) {
                var endAddress = content.endAddress.split(',');
                content.endAddress = endAddress[2];
              }
              if (res.list[i].createDate){
                res.list[i].createDate=res.list[i].createDate.substr(5,11);
              }
            }
            res.list[i]["content"]=content;
          }
            this.setData({
                list: this.data.list.concat(res.list),
                // hasMoreData: res.list.length>0?0:1,
            })

             this.stopRefreshFn({
                 list: this.data.list,
                 pageCount:res.pageCount
             })
        }
        
          // if(res.content) {
          //   var len=res.content.length;
          //   for (let i = 0;i < len; i++) {
          //     if(res.content[i]["remark"]!=null){
          //       res.content[i]["remark_arr"] = res.content[i]["remark"].split(',');
          //     }else{
          //       res.content[i]["remark_arr"]='';
          //     }
              
          //     var content = base.jsonToObj(res.content[i]["content"]);
          //     if (this.data.type==13){
          //       if (content.startAddress){
          //         var startAddress = content.startAddress.split(',');
          //         content.startAddress = startAddress[2];
          //       }
          //       if (content.endAddress) {
          //         var endAddress = content.endAddress.split(',');
          //         content.endAddress = endAddress[2];
          //       }
          //       if (res.content[i].createDate){
          //         res.content[i].createDate=res.content[i].createDate.substr(5,11);
          //       }
          //     }
          //     res.content[i]["content"]=content;
          //   }
  

          //   this.setData({
          //     list: this.data.list.concat(res.content),
          //     // hasMoreData: res.list.length>0?0:1,
          // })

          // //  this.stopRefreshFn({
          // //      list: this.data.list,
          // //      pageCount:res.pageCount
          // //  })

          //     // this.setData({
          //     //     list: this.data.list.concat(res.content),
          //     // })
          //   }

            
        })
    },
    onChange(e) {
        let index = e.detail.index
        let id = this.data.prePageData.childList[index].id
        this.setLoadInfo(id)
    },
    bind_yz_che(e){
      wx.redirectTo({
        url: '/pages/cheyz/index',
      })
    },
    setLoadInfo(sonType) {
        this.setData({
            list: [],
            num: 1,
          sonType: sonType
        })
        this.getData()
    },
    bind_close:function(){
      this.setData({
        che_yz_show:0
      });
    },
    onReady: function() {},
    onShow: function() {},

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {},

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {},

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {
        this.setLoadInfo(this.data.sonType)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
      // console.log(this.data.num, '9999')
      // if(this.data.num == 1) {
      //   return;
      // }
      if(!this.data.hasMore) {
        return;
      }
        this.setData({
          sonType: this.data.sonType,
          num: this.data.num+1,
      })
      this.getData()
    },

    stopRefreshFn(obj) {
      if(obj.pageCount == 0) {
        this.setData({
          hasMore:false
        })
        return;
      }
      console.log('sdlkfjsdfsld索拉卡大姐夫是地方', obj.pageCount, this.data.num)

      if(obj.pageCount == this.data.num) {
        console.log('sdlkfjsdfsld索拉卡大姐夫是地方', obj.pageCount == this.data.num)
        this.setData({
          hasMore:false
        })
        // wx.hideNavigationBarLoading()
        // 停止下拉动作
        // wx.stopPullDownRefresh()
        return;
      } 

        this.setData({
          hasMore:true
        })
      // }
      // wx.hideNavigationBarLoading()
      // 停止下拉动作
      // wx.stopPullDownRefresh()
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {}
})
